
/******************************************************************************
* build moving pattern from ACS and CPS
*****************************************************************************/


clear all
set more off
global proj "/Users/hanyang/tu2"
global dataRoot "${proj}/Data"


** CPS
use "${dataRoot}/Raw/cps", clear


* migration pattern
foreach t in "1"{
	gen mig_`t'y_same_hs   = migrate`t'==1
	gen mig_`t'y_within_ct = migrate`t'==3
	gen mig_`t'y_across_ct_within_st = migrate`t'==4
	gen mig_`t'y_across_st = migrate`t'==5
	gen mig_`t'y_abroad    = migrate`t'==6
}
gen mig_5y_same_hs   = migrate5==10
gen mig_5y_within_st = migrate5==20 & migsta5==statefip
gen mig_5y_across_st = migrate5==20 & migsta5!=statefip
gen mig_5y_abroad    = migrate5==60



* year level migration mattern
collapse mig_* [w=asecwt] , by(year)
	
* exclude those move from abroad
rename mig_1y_abroad abroad	
foreach var of varlist mig_1y_*{
	replace `var' = `var'/(1-abroad)
}
drop abroad

rename mig_5y_abroad abroad	
foreach var of varlist mig_5y_*{
	replace `var' = `var'/(1-abroad)
}
drop abroad
keep if inlist(year, 2005, 2015)
compress
save "${dataRoot}/Clean/cps_migration_year", replace






** ACS

**** moving pattern from annual acs survey
foreach yr of numlist 2005 2015{
	use "${dataRoot}/Raw/IPUMS_ACS_05_16", clear

	keep if year==`yr'

	replace rent = . if rent==0
	if `yr'<=2011{
		joinby statefip puma using "${proj}/Data/Crosswalk/puma_cz_cross_2000"
	}
	else{
		joinby statefip puma using "${proj}/Data/Crosswalk/puma_cz_cross_2010"
	}
	rename cz90 cz

	gen moved_any = (migrate1!=0 & migrate1!=9 &migrate1!=1)
	gen moved_state = (migrate1==3)
	
	//Adjust weight
	replace perwt = perwt*county_prop_inpuma

	collapse (mean) moved_*  [aw=perwt], by(year) fast
	save "$dataRoot/Clean/acs_migration_`yr'.dta", replace
}

* collapse at cz level
clear
gen year=.
foreach yr of numlist 2005 2015{
	append using "$dataRoot/Clean/acs_migration_`yr'.dta"
}
save "$dataRoot/Clean/acs_migration_year.dta", replace

